-
Notifications
You must be signed in to change notification settings - Fork 282
add profile_demo.py and add synchronize in infer_loop #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @hiworldwzj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on improving the robustness and observability of the inference batching process. It ensures that all CUDA operations related to adding requests are explicitly synchronized, which can prevent potential race conditions or data inconsistencies. Additionally, it provides a new profiling utility to help developers analyze and optimize the performance of CUDA and CPU operations within the system. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a profiling script and adds a CUDA synchronization point. My review focuses on improving the robustness of the new script and discussing the performance implications of the added synchronization, suggesting a more efficient alternative if applicable.
| slave_req: InferReq = slave_req | ||
| slave_req.related_master_req = master_req | ||
|
|
||
| torch.cuda.current_stream().synchronize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding torch.cuda.current_stream().synchronize() ensures that all previous asynchronous CUDA operations on the current stream are completed before proceeding. While this guarantees correctness, it can introduce a performance penalty by stalling the CPU, which might be undesirable on a critical path like request handling. If this synchronization is intended to manage dependencies between different CUDA streams, consider using torch.cuda.Event for a more efficient, non-blocking synchronization. If all related operations are on the same stream, this call might be unnecessary due to in-stream ordering guarantees.
| import torch | ||
| import numpy as np | ||
| from torch.profiler import profile, record_function, ProfilerActivity | ||
|
|
||
| torch.cuda.synchronize() | ||
| with profile( | ||
| activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], | ||
| record_shapes=False, | ||
| profile_memory=False, | ||
| on_trace_ready=torch.profiler.tensorboard_trace_handler("./log/"), | ||
| ) as prof: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The torch.profiler.tensorboard_trace_handler will write trace files to the ./log/ directory. If this directory does not exist, the script will fail with a FileNotFoundError. It's good practice to ensure the directory exists before using it. Using a variable for the log directory also improves readability and maintainability.
| import torch | |
| import numpy as np | |
| from torch.profiler import profile, record_function, ProfilerActivity | |
| torch.cuda.synchronize() | |
| with profile( | |
| activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], | |
| record_shapes=False, | |
| profile_memory=False, | |
| on_trace_ready=torch.profiler.tensorboard_trace_handler("./log/"), | |
| ) as prof: | |
| import torch | |
| import numpy as np | |
| from torch.profiler import profile, record_function, ProfilerActivity | |
| import os | |
| log_dir = "./log/" | |
| os.makedirs(log_dir, exist_ok=True) | |
| torch.cuda.synchronize() | |
| with profile( | |
| activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], | |
| record_shapes=False, | |
| profile_memory=False, | |
| on_trace_ready=torch.profiler.tensorboard_trace_handler(log_dir), | |
| ) as prof: |
No description provided.